package com.hxd.otc.module.home.activity;

import android.content.Intent;
import android.view.View;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseRecyclerActivity;
import com.hxd.otc.module.home.adapter.TicketListAdapter;
import com.hxd.otc.module.home.contract.TicketListContract;
import com.hxd.otc.module.home.presenter.TicketListPresenter;
import com.hxd.otc.utils.hyphenate.IListener;
import com.hxd.otc.utils.hyphenate.ListenerManager;
import com.hxd.otc.widget.TitleBar;
import com.hyphenate.helpdesk.domain.TicketEntity;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by lzy on 2019/1/15 0015.
 * description ${留言列表}
 */
public class TicketListActivity extends BaseRecyclerActivity<TicketListPresenter, TicketListAdapter, TicketEntity> implements TicketListContract.IView, IListener {

    @BindView(R.id.title_bar)
    TitleBar mTitleBar;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_ticket_list;
    }

    @Override
    protected TicketListAdapter createAdapter() {
        return new TicketListAdapter(this);
    }

    @Override
    protected TicketListPresenter createPresenter() {
        return new TicketListPresenter(this);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mTitleBar.setOnTitleBarItemClickListener(new TitleBar.OnTitleBarItemClickListener() {
            @Override
            public void onBack(View v) {
            }

            @Override
            public void onRight(View v) {
                createNewLeaveMessage();
            }
        });
        ListenerManager.getInstance().registerListener(this);
    }

    private void createNewLeaveMessage() {
        Intent intent = new Intent(this, NewLeaveMessageActivity.class);
        startActivity(intent);
    }

    @Override
    public void onItemClick(int position) {
        super.onItemClick(position);
        TicketEntity ticketEntity = mAdapter.getItem(position);
        Intent intent = new Intent();
        intent.setClass(this, TicketDetailActivity.class);
        intent.putExtra("ticket", ticketEntity);
        startActivity(intent);
    }

    @Override
    public void notifyEvent(String eventStr, Object eventObj) {
        if (eventStr.equals("clearTicketEvent")) {
            mAdapter.addAll(true, new ArrayList<TicketEntity>());
        } else {
            mPresenter.onRefresh();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ListenerManager.getInstance().unRegisterListener(this);
    }
}
